local jishi_viewas = fk.CreateSkill {
  name = "lvshi__jishi_viewas&",
  tags = { "Phase" },
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

-- 获取当前转换状态对应的卡牌名称
local function getCurrentCardInfo(player)
  local current_state = DIY.getSwitchState(player, "lvshi__jishi")
  local card_info = { "lvshi__dismantlement", "lvshi__snatch", "lvshi__rockfall" }
  return card_info[current_state] or card_info[1]
end

-- 检查目标是否符合下一个转换项的合法目标条件
local function checkNextTargetValid(player, target, next_card_name)
  if not target or target.dead then return false end
  
  if next_card_name == "lvshi__dismantlement" then
    -- 下一个过河拆桥：检查目标是否有手牌或装备
    return not target:isAllNude()
  elseif next_card_name == "lvshi__snatch" then
    -- 下一个顺手牵羊：检查目标是否有手牌或装备且距离允许
    return not target:isAllNude() and player:distanceTo(target) <= 1
  elseif next_card_name == "lvshi__rockfall" then
    -- 下一个落井下石：检查目标是否受伤
    return target:isKongcheng() or target.chained
  end
  return false
end

jishi_viewas:addEffect("viewas", {
  anim_type = "control",
  pattern = ".|.|.|.|lvshi__dismantlement,lvshi__snatch,lvshi__rockfall",
  prompt = function (self, player)
    local to = Fk:currentRoom():getPlayerById(player:getMark("jishi_from"))
    return "#lvshi__jishi-viewas:::".. getCurrentCardInfo(to)
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local to = Fk:currentRoom():getPlayerById(player:getMark("jishi_from"))
    local current_info = getCurrentCardInfo(to)
    local card = Fk:cloneCard(current_info, Card.NoSuit, 0)
    card.skillName = jishi_viewas.name
    return card
  end,
  
  before_use = function(self, player, use)
    -- 记录使用前的状态，用于后续判断是否需要重置
    use.extra_data = use.extra_data or {}
    local target = player.room:getPlayerById(player:getMark("jishi_from"))
    DIY.changeSwitchState(target, "lvshi__jishi", true)
    if use.tos and #use.tos > 0 then
      for _, to in ipairs(use.tos) do
        if not checkNextTargetValid(player, to, getCurrentCardInfo(target)) then
          use.extra_data.jishi_state_before = {to = to, status = "false"}
          break
        end
      end
    end
  end,
  
  after_use = function(self, player, use)
    local room = player.room
    local status = nil
    if use.extra_data and use.extra_data.jishi_state_before then
      status = use.extra_data.jishi_state_before.status
    end
    -- 检查是否有目标变为下一个转换项的合法目标，若有则重置转换状态
    local should_reset = false
    local target = room:getPlayerById(player:getMark("jishi_from"))
    if use.tos and #use.tos > 0 and status == "false" and use.extra_data and use.extra_data.jishi_state_before then
      for _, to in ipairs(use.tos) do
        if checkNextTargetValid(player, to, getCurrentCardInfo(target))
         and use.extra_data.jishi_state_before.to == to then
          should_reset = true
          break
        end
      end
    end
    
    if should_reset then
      -- 重置转换状态（相当于本次不消耗转换次数）
      player:clearSkillHistory(jishi_viewas.name, Player.HistoryPhase)
      room:sendLog{
        type = "#lvshi__jishi-reset",
        from = player.id,
        arg = "lvshi__jishi",
        toast = true,
      }
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(jishi_viewas.name, Player.HistoryPhase) == 0
  end,
})

Fk:loadTranslationTable{
  ["lvshi__jishi_viewas&"] = "掎势",
  ["#lvshi__jishi-viewas"] = "掎势：你可视为使用【%arg】",
  [":lvshi__jishi_viewas&"] = "阶段技，转换技，你可视为使用①【过河拆桥】②【顺手牵羊】③【落井下石】。若此牌目标之一因此变为符合下一个转换项合法目标，重置发动次数。",
  ["#lvshi__jishi-reset"] = "【%arg】被重置",
}

return jishi_viewas